#include "BinaryTreeMinDepth.hpp"
#include "leetcode.h"

uint32_t BinaryTreeMinDepth( TreeNode* root )
{
    uint32_t              res{};
    std::queue<TreeNode*> q{};
    q.push( root );

    if ( !q.empty() ) {
        res = 1;
    }
    TreeNode* back{ q.back() };
    while ( !q.empty() ) {
        TreeNode* cur = q.front();
        q.pop();
        if ( cur->left == std::nullptr_t() && cur->right == std::nullptr_t() ) {
            break;
        }
        else {
            if ( cur->left != std::nullptr_t() ) {
                q.push( cur->left );
            }
            if ( cur->right != std::nullptr_t() ) {
                q.push( cur->right );
            }
        }
        if ( cur == back ) {
            back = q.back();
            ++res;
        }
    }

    return res;
}